'use strict';
import Base from './base.js';
export default class extends Base {
  // 获取一级|二级项目类别总销量
  async getsubjectfeatAction() {
    const shopid = this.shop_userinfo.departmentid;

    const times = JSON.parse(this.post('times'));
    let begintime = Math.floor(new Date().setHours(0, 0, 0, 0) / 1000);
    let endtime = Math.floor(new Date().setHours(0, 0, 0, 0) / 1000) + 86400;
    if (times && (times[0] != null && times[1] != null)) {
      begintime = Math.floor(new Date(times[0]).setHours(0, 0, 0, 0) / 1000);
      endtime = Math.floor(new Date(times[1]).setHours(0, 0, 0, 0) / 1000) + 86400;
    };
    let departmentid = await this.model('or_department').where({id: shopid}).getField('pid', true)

    const data = await this.model().query('SELECT sj.id , sj.name , sj.pid, IFNULL(sum(aw.num),0) as allnum  from think_or_subjectcategory sj LEFT JOIN think_or_subject s on sj.id = s.categoryid left join (SELECT os.typeid, os.num from think_or_shop_order o LEFT JOIN think_or_shop_orderinfo os on o.id = os.orderid where os.departmentid = ' + shopid + ' and os.type = 2 and o.ordertype = 2 and o.status > 0 and o.addtime>=' + begintime + ' and o.addtime<' + endtime + ') aw on s.id = aw.typeid where sj.departmentid = ' + departmentid + ' GROUP BY sj.id');
    return this.json(data);
  }

  // 获取具体的项目销量
  async getspecificfeatAction() {
    const shopid = this.shop_userinfo.departmentid;
    const times = JSON.parse(this.post('times'));
    let begintime = Math.floor(new Date().setHours(0, 0, 0, 0) / 1000);
    let endtime = Math.floor(new Date().setHours(0, 0, 0, 0) / 1000) + 86400;
    if (times && (times[0] != null && times[1] != null)) {
      begintime = Math.floor(new Date(times[0]).setHours(0, 0, 0, 0) / 1000);
      endtime = Math.floor(new Date(times[1]).setHours(0, 0, 0, 0) / 1000) + 86400;
    };

    const data = await this.model().query('SELECT sb.name,sb.categoryid, IFNULL(sum(aw.num),0) as total FROM `think_or_subject` sb LEFT JOIN ( SELECT so.typeid, so.num FROM `think_or_shop_orderinfo` so LEFT JOIN `think_or_shop_order` s on so.orderid = s.id WHERE so.departmentid = ' + shopid + ' AND so.type = 2 AND s.ordertype = 2 and s.status > 0 AND s.addtime>=' + begintime + ' and s.addtime<' + endtime + ') aw on aw.typeid = sb.id GROUP BY sb.id');
    return this.json(data);
  }
}
